package org.sport.logic;

import org.sport.dbInterface.AwardOperation;
import org.sport.dbInterface.UserOperation;
import org.sport.dbInterface.UserPrivateOperation;
import org.sport.init.SportClientSymbol;
import org.sport.init.SportServerSymbol;
import org.sport.model.entityModel.AwardtableEntity;
import org.sport.model.entityModel.UserprivatetableEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.model.jsonModel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by zehong
 *
 * 比如一个User登录，前端传来一个JsonUser，该层进行数据解读，调用对应DBInterface进行数据查询，获取数据之后将数据进行对比，可能出现密码错误、该用户名不存在等错误
 */
@Service
public class Logic_login {

    //    UserOperation userOperation = new UserOperationImp();
    @Autowired
    UserOperation userOperation;

    @Autowired
    LogicUtils logicUtils;

    @Autowired
    UserPrivateOperation userPrivateOperation;

    @Autowired
    AwardOperation awardOperation;


    //登陆
    public Json_0_sign_in SignIn(Json_0_sign_in json_0_sign_in)
    {
        System.out.println("进入Logic-login-SignIn");

        UsertableEntity usertosearch2=logicUtils.findUser(json_0_sign_in.getPhoneNumber());
        //要进行判空，看user是否找到
        if(usertosearch2== null)
        {
            System.out.println("没找到对应user");
            json_0_sign_in.setResult(SportClientSymbol.Login.SIGNIN_RESULT_FAIL_USERNOTEXIT);
        } else {
            System.out.println("找到对应user");
            System.out.println("密码："+usertosearch2.getPassword());
            if(usertosearch2.getPassword().equals(json_0_sign_in.getPassword()))
            {
                json_0_sign_in.setResult(SportClientSymbol.Login.SIGNIN_RESULT_SUCCESS);
                System.out.println("登录成功");

                //同时检查有没有他自己的userprivate表，没有的话新建一个
                UserprivatetableEntity userprivate=userPrivateOperation.findUserPriByUserId(usertosearch2);
                if (userprivate==null){
                    UserprivatetableEntity userprivate2=new UserprivatetableEntity();
                    userprivate2.setUsertableByUserId(usertosearch2);
                    userPrivateOperation.addUserPri(userprivate2);
                    System.out.println("新建用户private表成功");
                }

                //同时检查有没有他自己的award表，没有的话新建一个
                AwardtableEntity awardTemp=new AwardtableEntity();
                awardTemp.setUsertableByUserId(usertosearch2);
                AwardtableEntity award=awardOperation.findByUserId(awardTemp);
                if (award==null){
                    AwardtableEntity awardReal=new AwardtableEntity();
                    awardReal.setTot(0);
                    awardReal.setUsertableByUserId(usertosearch2);
                    awardOperation.addOneData(awardReal);
                    System.out.println("新建用户award表成功");
                }
            } else
            {
                System.out.println("密码错误");
                json_0_sign_in.setResult(SportClientSymbol.Login.SIGNIN_RESULT_FAIL_WRONGPASSWORD);
            }
        }

        return json_0_sign_in;
    }


    //注册
    public Json_0_sign_up SignUp(Json_0_sign_up json_0_sign_up)
    {

        boolean signupStatus = false;
        int     signupcase  =  0;           //注册的各种状态
        System.out.println("进入Logic-login-SignUp");

        UsertableEntity usertosearch = new UsertableEntity();
        usertosearch.setPhoneNumber(json_0_sign_up.getPhoneNumber());
        usertosearch.setPassword(json_0_sign_up.getPassword());
        //先查找是否有一样的用户名
        UsertableEntity usertosearch2 = userOperation.findUserByPhone(usertosearch);
        //为空表示没有相同用户名
        if(usertosearch2 == null) {
            //没有相同用户名用户就开始注册
            signupStatus = userOperation.addUserEntity(usertosearch);
            if (signupStatus == false){
                signupcase = 2;
            }
            else{
                //注册成功
                signupcase = 3;
            }
        }
        else
        {
            signupcase = 1;
        }

        switch (signupcase)
        {
            case 1:
                json_0_sign_up.setResult(SportClientSymbol.Login.SIGNUP_RESULT_FAIL_USERNAMEREPEAT);
                break;
            case 2:
                System.out.println("数据库添加错误");
                json_0_sign_up.setResult(SportClientSymbol.Login.SIGNUP_RESULT_FAIL_DATABASEWRONG);
                break;
            case 3:
                json_0_sign_up.setResult(SportClientSymbol.Login.SIGNUP_RESULT_SUCCESS);
                break;
            default:
                break;
        }

        return json_0_sign_up;
    }

    //注册后进行初步的个人设置
    public Json_0_set Set(Json_0_set json_0_set) {

        System.out.println("进入Logic-login-Set");

        //找到该用户
        UsertableEntity usertoset = logicUtils.findUser(json_0_set.getPhoneNumber());

        if (usertoset==null){
            System.out.println("没找到对应user");
            json_0_set.setResult(SportClientSymbol.Login.SET_RESULT_FAIL_USERNOTEXIT);
        }else {
            //设置昵称
            usertoset.setName(json_0_set.getNickname());
            //给用户头像起个名字
            String iconName=json_0_set.getPhoneNumber()+"_"+new Date().getTime()+".jpg";
            //得到用户头像的String类型
            //String imageString=JsonImageString.getImageStr("E:\\IntelliJ IDEA\\code\\tclhomework_git\\img\\1_1506500752800.jpg");
            //获取用户头像，并保存到服务器图片地址中
            boolean getImageResult=false;
            boolean getImageResult2=false;
            try{
                //getImageResult= JsonImageString.generateImage(json_0_set.getIconString(), SportServerSymbol.tomcatImgPath+"\\"+iconName);
                //getImageResult2= JsonImageString.generateImage(json_0_set.getIconString(), SportServerSymbol.ImgStoragePath+"\\"+iconName);
                getImageResult= JsonImageString.generateImage(json_0_set.getIconString(), SportServerSymbol.tomcatImgPath+"/"+iconName);
                getImageResult2= JsonImageString.generateImage(json_0_set.getIconString(), SportServerSymbol.ImgStoragePath+"/"+iconName);
            }catch (Exception e){
                e.printStackTrace();
            }
            if(getImageResult&&getImageResult2){
                //获取图片成功
                String iconPath="/0/upload_img/"+iconName;
                System.out.println("图片在服务器地址为"+iconPath);
                json_0_set.setIconURL(iconPath);
                //设置头像
                usertoset.setHeadIcon(iconPath);

                //更新用户信息
                boolean setResult=userOperation.updateUserEntity(usertoset);
                if (setResult){
                    //更新成功
                    System.out.println("更新用户信息成功");
                    json_0_set.setResult(SportClientSymbol.Login.SET_RESULT_SUCCESS);
                }else {
                    //更新失败
                    System.out.println("更新用户信息失败");
                    json_0_set.setResult(SportClientSymbol.Login.SET_RESULT_FAIL_DATABASEWRONG);
                }
            }else{
                //获取用户头像失败
                System.out.println("获取用户头像失败");
                json_0_set.setResult(SportClientSymbol.Login.SET_RESULT_FAIL_IMAGEFAIL);
            }


        }


        return json_0_set;

    }

    //忘记密码
    public Json_0_forget_passwd ForgetPassward(Json_0_forget_passwd json_0_forget_passwd) {

        System.out.println("进入Logic-login-ForgetPassward");

        UsertableEntity usertoupdate = logicUtils.findUser(json_0_forget_passwd.getPhoneNumber());

        if (usertoupdate==null){
            System.out.println("没找到对应user");
            json_0_forget_passwd.setResult(SportClientSymbol.Login.FORGETPASSWORD_RESULT_FAIL_USERNOTEXIT);
        }else {
            usertoupdate.setPassword(json_0_forget_passwd.getNewPassword());

            //更新个人信息
            boolean result=userOperation.updateUserEntity(usertoupdate);
            if (result){
                System.out.println("更新成功");
                json_0_forget_passwd.setResult(SportClientSymbol.Login.FORGETPASSWORD_RESULT_SUCCESS);
            }else {
                System.out.println("更新失败");
                json_0_forget_passwd.setResult(SportClientSymbol.Login.FORGETPASSWORD_RESULT_FAIL_DATABASEWRONG);
            }
        }

        return json_0_forget_passwd;

    }

}

